﻿using AutoMapper;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.EquipmentLayoutManageDomain
{
    public class EquipmentLayoutTypeService : IEquipmentLayoutTypeService
    {
        private readonly ILogUtil _logUtil;
        private readonly IEquipmentLayoutTypeRepository _EquipmentLayoutTypeRepository;
        private readonly IMapper _Mapper;
        private readonly ISnowflake _Snowflake;


        public EquipmentLayoutTypeService(ISnowflake snowflake, IEquipmentLayoutTypeRepository EquipmentLayoutTypeRepository, ILogUtil logUtil, IMapper mapper)
        {
            _EquipmentLayoutTypeRepository = EquipmentLayoutTypeRepository;
            _logUtil = logUtil;
            _Mapper = mapper;
            _Snowflake = snowflake;
        }

        public async Task<long> AddEquipmentLayoutTypeAsync(EquipmentLayoutTypeInfoDto EquipmentLayoutTypeInfoDto)
        {
            var EquipmentLayoutTypeInfo = _Mapper.Map<EquipmentLayoutTypeInfoDto, EquipmentLayoutTypeInfo>(EquipmentLayoutTypeInfoDto);
            long id = _Snowflake.GetId();
            EquipmentLayoutTypeInfo.ID = id;
            bool result = await _EquipmentLayoutTypeRepository.InsertAsync(EquipmentLayoutTypeInfo);
            return result ? id : 0;
        }

        public async Task<bool> DeleteEquipmentLayoutTypeAsync(long ID)
        {
            bool result = await _EquipmentLayoutTypeRepository.DeleteAsync(it => it.ID == ID);
            return result;
        }

        public async Task<bool> UpdateEquipmentLayoutTypeAsync(EquipmentLayoutTypeInfoDto EquipmentLayoutTypeInfoDto)
        {
            var equipmentLayoutTypeInfo = _Mapper.Map<EquipmentLayoutTypeInfoDto, EquipmentLayoutTypeInfo>(EquipmentLayoutTypeInfoDto);
            bool result = await _EquipmentLayoutTypeRepository.UpdateAsync(equipmentLayoutTypeInfo);
            return result;
        }

        public async Task<PageDto<EquipmentLayoutTypeInfoDto>> GetEquipmentLayoutTypeAsync(QueryEquipmentLayoutTypeInfoDto queryEquipmentLayoutTypeInfoDto)
        {
            var pageModel = new PageModel() { PageIndex = queryEquipmentLayoutTypeInfoDto.PageIndex, PageSize = queryEquipmentLayoutTypeInfoDto.PageSize };

            var equipmentTypeID = queryEquipmentLayoutTypeInfoDto.EquipmentTypeID;
            var typeCode = queryEquipmentLayoutTypeInfoDto.TypeCode;
            var typeName = queryEquipmentLayoutTypeInfoDto.TypeName;
            var exp = Expressionable.Create<EquipmentLayoutTypeInfo, EquipmentTypeInfo>();
            exp.AndIF(!string.IsNullOrEmpty(typeCode), (a, b) => a.TypeName.Contains(typeCode));
            exp.AndIF(!string.IsNullOrEmpty(typeName), (a, b) => a.TypeCode == typeName);
            exp.AndIF(equipmentTypeID.HasValue && equipmentTypeID != 0, (a, b) => a.EquipmentTypeID == equipmentTypeID);
            var result = await _EquipmentLayoutTypeRepository.GetEquipmentLayoutTypeAsync(exp.ToExpression(), pageModel);
            return result;
        }

        public async Task<EquipmentLayoutTypeInfoDto> GetEquipmentLayoutTypeByIDAsync(long ID)
        {
            var result = await _EquipmentLayoutTypeRepository.GetByIdAsync(ID);
            var vModel = _Mapper.Map<EquipmentLayoutTypeInfo, EquipmentLayoutTypeInfoDto>(result);
            return vModel;
        }
    }
}
